package com.ruoyi.order.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.OrdCart;
import com.ruoyi.order.service.IOrdCartService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 购物车Controller
 *
 * @author admin
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/order")
public class OrdCartController extends BaseController {
    @Autowired
    private IOrdCartService ordCartService;

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(OrdCart ordCart) {
        startPage();
        List<OrdCart> list = ordCartService.selectOrdCartList(ordCart);
        return getDataTable(list);
    }

    @RequiresPermissions("order:oder:shop")
    @GetMapping("/shop")
    public List list(int productId) {
        List list = ordCartService.queryAllproduct(productId);
        return list;
    }




    /**
     * 导出购物车列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrdCart ordCart) {
        List<OrdCart> list = ordCartService.selectOrdCartList(ordCart);
        ExcelUtil<OrdCart> util = new ExcelUtil<OrdCart>(OrdCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId) {
        return success(ordCartService.selectOrdCartByCartId(cartId));
    }

    /**
     * 新增购物车
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrdCart ordCart) {
        ordCart.setCartTime(new Date());
        ordCart.setModifyTime(new Date());
        return toAjax(ordCartService.insertOrdCart(ordCart));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrdCart ordCart) {
        return toAjax(ordCartService.updateOrdCart(ordCart));
    }

    /**
     * 删除购物车
     */
//    @RequiresPermissions("order:order:remove")
//    @Log(title = "购物车", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{cartIds}")
//    public AjaxResult remove(@PathVariable Long[] cartIds) {
//        return toAjax(ordCartService.deleteOrdCartByCartIds(cartIds));
//    }

    @RequiresPermissions("order:order:delete")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cartId}")
    public AjaxResult delete(@PathVariable Long cartId) {
        return toAjax(ordCartService.deleteOrdCartByCartId(cartId));
    }
}
